import {_decorator, Component, director, instantiate, Label, Node, Prefab, ScrollView, UITransform} from "cc";
import PoolRankItem from "db://assets/script/game/mainui/ranks/pool-rank-item";
import {FishPoolRankResult_PoolRank} from "db://assets/proto/new-world-api/v1/fish";
import {GameEvent} from "db://assets/script/game-event";
import {UserFishRanksResultEvent} from "db://assets/script/common/events/fish-events/user-fish-ranks-result-event";
import {UserFishRanksRequestEvent} from "db://assets/script/common/events/fish-events/user-fish-ranks-request-event";
import Long from "long";
import {configsHelper} from "db://assets/script/configs-helper";

const {ccclass, property} = _decorator;

@ccclass("CommonRanks")
export default class CommonRanks extends Component {
    @property(ScrollView)
    scrollView!: ScrollView;

    @property(Prefab)
    itemPrefab!: Prefab;

    @property(Label)
    title!: Label

    @property(Node)
    loadingIndicator!: Node; // 加载提示节点


    private currentPage: number = 1;
    private isLoading: boolean = false;
    private hasMoreData: boolean = true;
    private rankType: number = 0;
    private zoneId!: Long;

    start() {
        this.scrollView.node.on(ScrollView.EventType.SCROLL_TO_BOTTOM, this.loadNextPage, this);
        // this.scrollView.stopAutoScroll()
        this.onEvent()
    }

    onEvent() {
        director.on(GameEvent.userFishRanksResult, this.rankDataCome, this)
    }

    offEvent() {
        director.off(GameEvent.userFishRanksResult, this.rankDataCome, this)
    }

    loadRank(rankType: number, zoneId: Long) {
        this.rankType = rankType;
        this.zoneId = zoneId;
        const zc = configsHelper.loadZoneConfig(this.zoneId)
        this.title.string = '天骄榜·' + zc!.zoneName
        this.loadPage(1)
        this.show()
    }

    show() {
        this.node.active = true;
    }

    hide() {
        this.node.active = false;
    }

    // 加载指定页数据
    loadPage(page: number) {
        // if (this.isLoading || !this.hasMoreData) return;
        this.showLoading()
        director.emit(GameEvent.userFishRequestRanks, new UserFishRanksRequestEvent(this.rankType, this.zoneId, page, 10))
    }

    rankDataCome(event: UserFishRanksResultEvent) {
        this.currentPage = event.resp.currentPage
        this.hasMoreData = event.resp.hasNext
        this.createItems(event.resp.list)
        if (this.currentPage == 1) this.scrollView.scrollToTop()
        this.hideLoading()
    }

    loadNextPage() {
        if (this.hasMoreData && !this.isLoading) this.loadPage(this.currentPage + 1);
    }

    // 创建列表项
    createItems(data: FishPoolRankResult_PoolRank[]) {
        // let lineHeight = 0
        for (let i = 0; i < Math.min(data.length, 10); i++) {
            const itemData = data[i]
            const itemNode = instantiate(this.itemPrefab);
            itemNode.getComponent(PoolRankItem)!.init(itemData.rank, itemData.fish!.fish!); // 初始化列表项
            this.scrollView.content!.addChild(itemNode);
        }

        // 更新content高度（垂直滚动）

        // this.scrollView.content.getComponent(UITransform).height = this.scrollView.content.children.length * lineHeight; // 假设每项高度100
        console.log('after loaded height: ' + this.scrollView.content!.getComponent(UITransform)!.height)
    }

    showLoading() {
        this.isLoading = true
        if (this.loadingIndicator) this.loadingIndicator.active = true;
    }

    hideLoading() {
        this.isLoading = false
        if (this.loadingIndicator) this.loadingIndicator.active = false;
    }

    onDestroy() {
        this.offEvent()
    }
}